<?php

/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-01-20
 * 版    本：1.0.0
 * 功能说明：用户控制器。
 *
 * */

namespace Qwadmin\Controller;

//use Common\Controller\openerp;

class GoodsopController extends ComController {

    private $opmodel;

    function __construct() {
        parent::__construct();
        $this->opmodel = D("goods_op");
    }

    function index() {

        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';


        $where = array();
        $poname = I("get.poname");
        $stie_id = I("get.stie_id");
        $store_id = I("get.store_id");
        $rsp = I("get.rsp");

        if($poname && !empty($poname)){
            $where['op'] = $poname;
        }
        if($rsp && !empty($rsp)){
            $where['rsp'] = $rsp;
        }
        if($stie_id && !empty($stie_id)){
            $where['site_id'] = $stie_id;
        }
        if($store_id && !empty($store_id)){
            $where['store_id'] = $store_id;
        }
        
        if ($where) {
            $list = $this->opmodel
                    ->where($where)
                    ->order("optime desc")
                    ->select();
        } else {
            $pagesize = 20; #每页数量
            $offset = $pagesize * ($p - 1); //计算记录偏移量
            $count = $this->opmodel
                            ->where($where)->count();
            $list = $this->opmodel
                    ->where($where)->limit($offset . ',' . $pagesize)
                    ->order("optime desc")
                    ->select();
            $page = new \Think\Page($count, $pagesize);
            $page = $page->show();
        }
        $mm=M("Goods");
        foreach($list as $key=>$vvv){
            $list[$key]['sku']=$mm->where(array("id"=>$vvv['goods_id']))->getField("sku");
        }
        $POnameList = $this->opmodel->group("op")->field("op")->cache(true)->select();
        $rspList = $this->opmodel->group("rsp")->field("rsp")->cache(true)->select();
        $site_idList = $this->opmodel->group("site_id")->field("site_id")->cache(true)->select();
        $store_idList = $this->opmodel->group("store_id")->field("store_id")->cache(true)->select();
//        var_dump($list);die;
        $this->assign('rspList', $rspList);
        $this->assign('POnameList', $POnameList);
        $this->assign('site_idList', $site_idList);
        $this->assign('store_idList', $store_idList);
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->display();
    }
    
    function retry(){
        $gid=$_GET['ids'];
        $ids=$_POST['ids'];
        if($gid){
            $ids[]=$gid;
        }
        if(!$ids){
            $this->error("操作的数据为空",U("index"));
        }
        
        $this->opmodel->where(array("id"=>array("in",$ids)))->save(array("rsp"=>""));

        $this->success("操作成功,正在跳转至start商品指引去更新商品",U("ShopSiteApi/runapi",array("apiid"=>12)));
        
    }
    
    function del() {
        $gid = I("get.id", 0);
        $ids = $_POST['ids'];
        if ($gid) {
            $ids[] = $gid;
        }
        if (!$ids) {
            $this->error("操作的数据为空", U("index"));
        }


        if ($this->opmodel->where(array("id" => array("in", $ids)))->delete()) {
            $this->success("删除成功");
        } else {
            $this->error("失败");
        }
    }

    
    function insertshop_api(){
        $model=M('shop_api');
        $dataes=$model->where(array("shop_id"=>'10001'))->select();
        echo "<pre>";var_dump($dataes);
        foreach($dataes as $dd){
            $insertdata=array(
                'shop_id'=>"10002",
                'flag'=>$dd['flag'],
                'url'=>"http://vip.shenghuigo.com/api",
                'direct'=>$dd['direct'],
                'info'=>  str_replace(array("盛会购",'b2c'),array("盛会购vip",'vipb2b'), $dd['info']),
                'name'=> str_replace(array("盛会购",'b2c'),array("盛会购vip",'vipb2b'), $dd['name']),
                'in_data'=>$dd['in_data'],
                'execute_obj'=>$dd['execute_obj'],
                'method'=>$dd['method'],
                'm_indata'=>$dd['m_indata'],
            );
            
            if(!$model->where(array('shop_id'=>'10002',"flag"=>$dd['flag']))->find()){
                echo 22,"|";
                $model->add($insertdata);
            }
        }
        
    }
}
